/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package morseSide;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author Allan Helbling
 */
public class Decompose {

     
    private final String s = "\\s*((([^\"\\[\\]\\s]+))|(\\[([^\\]]+)\\])|(\\\"([^\"]+)\\\" ))\\s*";
    
    private ArrayList<String> decom(String aString) {
        Pattern p = Pattern.compile(s);
        Matcher m = p.matcher(aString);
        ArrayList<String> tmp = new ArrayList<String>();

        while (m.find()) {
            tmp.add(m.group(1));
        }
        //petit affichage pour verifier les valeurs du Vector
        System.out.println("Découpé : ");
//        for (int i = 0; i < tmp.size(); i++) {
//            System.out.println(tmp.get(i));
//        }
        
        return tmp;
    }
    /**
     * Gets the parameters of a message
     * @param aString The message to analyze
     * @return An ArrayList containing the parameters 
     */
    public ArrayList<String> getParams(String aString){
        ArrayList<String> l = new ArrayList<String>(); 
        ArrayList<String> tmp = new ArrayList<String>(); 
        tmp = decom(aString);
        
        for (int i = 1; i < tmp.size(); i+=3) {
            l.add(tmp.get(i));
        }
        System.out.println("Les paramètres :");  
        for (int i = 0; i < l.size(); i++) {
            System.out.println("\t"+l.get(i));
        }
        
        return l;
    }
    
     /**
     * Gets the values of a message
     * @param aString The message to analyze
     * @return An ArrayList containing the values 
     */
    public ArrayList<String> getVals(String aString){
        ArrayList<String> l = new ArrayList<String>(); 
        ArrayList<String> tmp = new ArrayList<String>(); 
        tmp = decom(aString);
        
        for (int i = 3; i < tmp.size(); i+=3) {
            l.add(tmp.get(i).substring(0, tmp.get(i).length()-1)); //On supprime le caractere suivant le nombre, en general "," ou "}"
        }
         System.out.println("Les valeurs des paramètres :");
        for (int i = 0; i < l.size(); i++) {
            System.out.println("\t"+l.get(i));
        }
        
        return l;
    }
    
    public HashMap<String, Integer> getStream (String aString){
    	HashMap<String, Integer> hm = new HashMap<String, Integer>();
        ArrayList<String> tmp = new ArrayList<String>(); 
        tmp = decom(aString);
    	
    	for (int i = 1; i < tmp.size(); i+=3) {
    		String val = tmp.get(i+2).substring(0, tmp.get(i).length()-1);
    		int intVal = Integer.parseInt(val.substring(0, val.indexOf(".")));
    		hm.put(tmp.get(i), intVal);
        }
        
    	return hm;
    }
}
